#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#include <queue>

using namespace std;

queue<char*> g_queue;
pthread_mutex_t g_mutex;
char* g_ch;

void* func_malloc(void* arg)
{
	printf("malloc....");
	g_ch = (char*)malloc(10);
	printf("over\n");

	pthread_mutex_lock(&g_mutex);
	g_queue.push(g_ch);
	pthread_mutex_unlock(&g_mutex);
	
	return NULL;
}

void* func_free(void* arg)
{
	while(true)
	{
		if(g_queue.size() > 0)
		{
			pthread_mutex_lock(&g_mutex);
			g_ch = g_queue.front();
			g_queue.pop();
			pthread_mutex_unlock(&g_mutex);
	
			printf("free....");
			free(g_ch);
			printf("over\n");
		}
		else
		{
			usleep(10);
		}
	}
	return NULL;
}

int main()
{
	pthread_t tid;
	pthread_mutex_init(&g_mutex, NULL);
	pthread_create(&tid, NULL, func_free, NULL);
	while(true)
	{
		func_malloc(NULL);
	}
	pthread_mutex_destroy(&g_mutex);
}
